//
//  LFShopCarPayViewController.m
//  LoveFruit
//
//  Created by libbbb on 16/9/23.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFShopCarPayViewController.h"
#import "LFShopCarPayTableView.h"
#import "LFShopCarPayCostCell.h"
#import "LFShopCarPayCostModel.h"
#import "LFShopCarPayFoodCell.h"
#import "LFShopCarFoodModel.h"
#import "LFCouponViewController.h"
#import "LdAccountOrderViewController.h"

@interface LFShopCarPayViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UILabel *lab;

@end

static NSString *couPonCellid = @"CouPonCellId";
static NSString *payStyleCellid = @"payStyleCellID";
static NSString *foodCellid = @"foodCellId";
static NSString *costCellid = @"costCellId";


@implementation LFShopCarPayViewController
{
    UIButton *selecteButton;
    NSMutableArray <UIButton *> *arrayBut;
}

-(void)setCellArrayM:(NSMutableArray *)cellArrayM{
    _cellArrayM = cellArrayM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    arrayBut = [NSMutableArray array];
    self.navigationItem.title = @"结算付款";
    UIBarButtonItem *backItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(backClick) icon:@"btn_backItem" highlighticon:nil];
    self.navigationItem.leftBarButtonItem = backItem;
    //初始化界面
    [self setupPayTableView];
    //初始化底部界面
    [self setupFootView];
}

- (void)setupFootView {
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width, 40));
    }];
    //实付金额Label
    UILabel *lab = [[UILabel alloc]init];
    self.lab = lab;
    [footView addSubview:self.lab];
    [self.lab setTextColor:[UIColor redColor]];
    lab.font = [UIFont systemFontOfSize:14];
//    lab.text = @"实付金额: $0.0";
    
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(footView);
        make.left.equalTo(footView).offset(10);
    }];
    
    //确认付款按钮
    UIButton *but = [[UIButton alloc]init];
    [but setTitle:@"确认付款" forState:UIControlStateNormal];
    but.titleLabel.font = [UIFont systemFontOfSize:14];
    [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [but setBackgroundColor:[UIColor colorWithHex:0xf47711]];
    [footView addSubview:but];
    
    [but mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footView);
        make.bottom.equalTo(footView);
        make.height.mas_equalTo(footView);
        make.width.mas_equalTo(100);
    }];
    [but addTarget:self action:@selector(butMoneyClick) forControlEvents:UIControlEventTouchUpInside];

}

- (void)setupPayTableView {
    LFShopCarPayTableView *payView = [[LFShopCarPayTableView alloc] init];
    payView.delegate = self;
    payView.dataSource =self;
//    payView.rowHeight = 30;
    //注册cell
    [payView registerClass:[UITableViewCell class] forCellReuseIdentifier:couPonCellid];
    [payView registerClass:[UITableViewCell class] forCellReuseIdentifier:payStyleCellid];
    [payView registerNib:[UINib nibWithNibName:@"LFShopCarPayFoodCell" bundle:nil] forCellReuseIdentifier:foodCellid];
    [payView registerNib:[UINib nibWithNibName:@"LFShopCarPayCostCell" bundle:nil] forCellReuseIdentifier:costCellid];
    
    
    [self.view addSubview:payView];

    //tableView约束
    [payView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-40);
    }];

    [payView reloadData];
}

#pragma mark - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            return 1;
        case 1:
            return 4;
        case 2:
            if (self.cellArrayM.count) {
                return self.cellArrayM.count;
            }
            return 1;
        case 3:
            return 1;
            
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:couPonCellid forIndexPath:indexPath];
        
        cell.imageView.image = [UIImage imageNamed:@"v2_submit_Icon"];
        cell.textLabel.text = @"1张优惠券";
        cell.textLabel.textColor = [UIColor redColor];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        //箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        //cell点击不变色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIButton *seeButton = [[UIButton alloc]init];
        [cell addSubview:seeButton];
        [seeButton setTitle:@"查看" forState:UIControlStateNormal];
        seeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [seeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [seeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.mas_centerY);
            make.right.equalTo(cell.mas_right).offset(-30);
        }];
        [seeButton addTarget:self action:@selector(gotoCoupon) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    else if (indexPath.section == 1) {
        
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:4];
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:payStyleCellid forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == 0) {
            
            cell.imageView.image = [UIImage imageNamed:@"微信"];
            cell.textLabel.text = @"微信支付";
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            UIButton *but = [[UIButton alloc]init];
            [cell addSubview:but];
            [but setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
            [but setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
            
            [but mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY);
                make.right.equalTo(cell.mas_right).offset(-10);
            }];
            
            [arrM addObject:but];
            
        }
        
        else if (indexPath.row == 1) {
            
            cell.imageView.image = [UIImage imageNamed:@"QQ钱包"];
            cell.textLabel.text = @"QQ钱包";
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            UIButton *but = [[UIButton alloc]init];
            [cell addSubview:but];
            [but setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
            [but setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
            
            [but mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY);
                make.right.equalTo(cell.mas_right).offset(-10);
            }];
            
            [arrM addObject:but];
        }
        
        else if (indexPath.row == 2) {
            
            cell.imageView.image = [UIImage imageNamed:@"支付宝支付"];
            cell.textLabel.text = @"支付宝支付";
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            UIButton *but = [[UIButton alloc]init];
            [cell addSubview:but];
            [but setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
            [but setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
            
            [but mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY);
                make.right.equalTo(cell.mas_right).offset(-10);
            }];
            
            [arrM addObject:but];
        }
        
        else if (indexPath.row == 3) {
            
            cell.imageView.image = [UIImage imageNamed:@"货到付款"];
            cell.textLabel.text = @"货到付款";
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            UIButton *but = [[UIButton alloc]init];
            [cell addSubview:but];
            [but setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
            [but setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
            
            [but mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY);
                make.right.equalTo(cell.mas_right).offset(-10);
            }];
            
            [arrM addObject:but];
        }
        arrayBut = arrM.copy;
        
        [arrayBut enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.tag = idx;
            [obj addTarget:self action:@selector(changeSelection:) forControlEvents:UIControlEventTouchUpInside];
        }];
        
        return cell;
    }
    else if (indexPath.section == 3 && indexPath.row == 0) {
        LFShopCarPayCostCell *cell = [tableView dequeueReusableCellWithIdentifier:costCellid forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.totalPriceLabel.text = [NSString stringWithFormat:@"$%zd",self.moeny];
        if (self.moeny < 30) {
            cell.totalPriceLabel.text = [NSString stringWithFormat:@"$%zd",(self.moeny + 5)];
            cell.distributionPriceLabel.text = [NSString stringWithFormat:@"$%d",5];
        }
        self.lab.text = [NSString stringWithFormat:@"实付金额: %@",cell.totalPriceLabel.text];
        return cell;
        
    }
    LFShopCarPayFoodCell *cell = [tableView dequeueReusableCellWithIdentifier:foodCellid forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.arrayDataModelsList = self.cellArrayM[indexPath.row];
//      cell.textLabel.text = @"请前往首页,选取商品";
    
    return cell;
}
#pragma mark - 用tableView的代理实现cell组头、组尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.1;
    }
    else  {
        return 30;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

#pragma mark - 设置组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    tableView.tintColor = [UIColor redColor];
    if (section == 1)  {
        return @"选择支付方式";
    }
    if (section == 2)  {
        return @"精选商品";
    }
    if (section == 3)  {
        return @"费用明细";
    }
    return nil;
}

#pragma mark - 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return 120;
    }
    return 40;
}

#pragma mark - 勾选按钮监听事件
- (void)changeSelection:(UIButton *)sender
{
    selecteButton.selected = NO;
    
    sender.selected = YES;
    
    selecteButton = sender;
}

#pragma mark - 查看优惠券按钮监听事件
- (void)gotoCoupon {
    
    NSLog(@"跳转到优惠券界面");
    LFCouponViewController *couponVC = [LFCouponViewController new];
    [self.navigationController pushViewController:couponVC animated:YES];
    
}

#pragma mark - 返回按钮监听事件
- (void)backClick {
    NSLog(@"返回");
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 确认付款监听事件
- (void)butMoneyClick {
    NSLog(@"确认付款");
    LdAccountOrderViewController *orderVC = [LdAccountOrderViewController new];
    [self.navigationController pushViewController:orderVC animated:YES];
}



@end
